package cn.wolfcode.crm.service.impl;

import cn.wolfcode.crm.domain.Customer;
import cn.wolfcode.crm.domain.Employee;
import cn.wolfcode.crm.domain.Systemdictionaryitem;
import cn.wolfcode.crm.mapper.CustomerMapper;
import cn.wolfcode.crm.query.QueryObject;
import cn.wolfcode.crm.service.ICustomerService;
import cn.wolfcode.crm.util.UserContext;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class CustomerServiceImpl implements ICustomerService {
    @Autowired
    private CustomerMapper customerMapper;

    public void saveOrUpdate(Customer entity) {
        if (entity.getId() == null) {
            //设置营销人员,录入时间,录入人员
            Employee currentUser = UserContext.getCurrentUser();
            entity.setSeller(currentUser);
            entity.setInputUser(currentUser);
            entity.setInputTime(new Date());
            entity.setStatus(Customer.POTENTIAL_CUSTOMERS);
            //保存
            customerMapper.insert(entity);
        } else {
            customerMapper.updateByPrimaryKey(entity);
        }
    }

    public List<Customer> list() {
        return customerMapper.selectAll();
    }

    public PageInfo<?> queryForList(QueryObject qo) {
        PageHelper.startPage(qo.getCurrentPage(),qo.getPageSize());// 只起一次作用
        List<?> list = customerMapper.selectForList(qo);
        return new PageInfo<>(list);
    }

    public void updateStatus(Customer entity) {
        customerMapper.updateStatus(entity);
    }

    public void updateFormal(Customer customer) {
        customerMapper.updateFormal(customer);
    }
}
